package dao

import (
	"ball/global"
	"ball/internal/model"
)

func CreateUser(user *model.Users) bool {
	if err := global.DB.Create(&user).Error; err != nil {
		return false
	}
	return true
}

func ListVideo() []model.Videos {
	var result []model.Videos
	if err := global.DB.Find(&result).Error; err != nil {
		return []model.Videos{}
	}
	return result
}

func Create[T any](data *T) bool {
	if err := global.DB.Create(data).Error; err != nil {
		return false
	}
	return true
}
func Delete[T any](data *T) bool {
	if err := global.DB.Delete(data).Error; err != nil {
		return false
	}
	return true
}
func Update[T any](data *T) bool {
	if err := global.DB.Updates(data).Error; err != nil {
		return false
	}
	return true
}

func GetOneByFields[T any](where *T, data *T) bool {

	if err := global.DB.Where(where).Find(data).Limit(1).Error; err != nil {
		return false
	}
	return true

}

func GetListByFields[T any](where *T, data *[]T) bool {

	if err := global.DB.Where(where).Find(data).Error; err != nil {
		return false
	}
	return true

}

func GetOneById[T any](id uint, data *T) bool {
	if err := global.DB.Where("id = ?", id).Find(data).Limit(1).Error; err != nil {
		return false
	}
	return true
}
func GetUser(tel string) model.Users {
	var result model.Users
	if err := global.DB.Where("tel=?", tel).Find(&result).Error; err != nil {
		return model.Users{}
	}
	return result
}
